package com.pavco.caribbeanvisit.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.pavco.caribbeanvisit.client.JsArray;
import com.pavco.caribbeanvisit.client.JsniPerson;
import com.pavco.caribbeanvisit.client.MyResources;
import com.pavco.caribbeanvisit.client.view.MainView;

public class GfcSignedInWidget extends Composite {

  private static GfcSignedInWidgetUiBinder uiBinder = GWT
      .create(GfcSignedInWidgetUiBinder.class);

  interface GfcSignedInWidgetUiBinder extends
      UiBinder<Widget, GfcSignedInWidget> {
  }
  
  MyResources resources = GWT.create(MyResources.class);
  @UiField Label gfcLabel;
  @UiField VerticalPanel gfcSignInPanel;
  @UiField HorizontalPanel gfcAnchorPanel;
  
  private MainView parent;
  
  public GfcSignedInWidget(MainView parent) {
    this.parent = parent;
    exportAll(this);
    initWidget(uiBinder.createAndBindUi(this));
  }
  
  public native void exportAll(GfcSignedInWidget x) /*-{
    $wnd.setSignedInWidgetText = function (dispName, signedIn) {
        $entry(x.@com.pavco.caribbeanvisit.client.ui.GfcSignedInWidget::setGfcSignedInWidgetText(Ljava/lang/String;Z)(dispName, signedIn));
    };
    $wnd.addViewerFriends = function (friendsObj) {
        $entry(x.@com.pavco.caribbeanvisit.client.ui.GfcSignedInWidget::addViewerFriends(Lcom/pavco/caribbeanvisit/client/JsArray;)(friendsObj));
    };
    $wnd.addSiteFriends = function (friendsObj) {
        $entry(x.@com.pavco.caribbeanvisit.client.ui.GfcSignedInWidget::addSiteFriends(Lcom/pavco/caribbeanvisit/client/JsArray;)(friendsObj));
    };
  }-*/;
  
  public native void executeLoginJs() /*-{
    $wnd.google.friendconnect.requestSignIn(); 
  }-*/;
  
  public native void executeSettingsJs() /*-{
    $wnd.google.friendconnect.requestSettings();
  }-*/;
  
  public native void executeInviteJs() /*-{
    $wnd.google.friendconnect.requestInvite();
  }-*/;
  
  public native void executeLogoutJs() /*-{
    $wnd.google.friendconnect.requestSignOut();
  }-*/;
  
  public void setGfcSignedInWidgetText(String displayName, boolean signedIn) {
    if (signedIn) {
      Anchor logoutLink = new Anchor("Sign out");
      logoutLink.setHref("#");
      logoutLink.addClickHandler(new ClickHandler() {
        
        @Override
        public void onClick(ClickEvent event) {
          executeLogoutJs();
        }
      });
      Anchor inviteLink = new Anchor("Invite");
      inviteLink.addClickHandler(new ClickHandler() {
        
        @Override
        public void onClick(ClickEvent event) {
          executeInviteJs();
        }
      });
      Anchor settingsLink = new Anchor("Settings");
      settingsLink.addClickHandler(new ClickHandler() {
        
        @Override
        public void onClick(ClickEvent event) {
          executeSettingsJs();
        }
      });
      gfcAnchorPanel.clear();
      gfcAnchorPanel.add(settingsLink);
      gfcAnchorPanel.add(inviteLink);
      gfcAnchorPanel.add(logoutLink);
      //mainPanel.getElementById("gfcNotSignedIn").removeFromParent();
    } else {
      Anchor signInLink = new Anchor("Sign In");
      signInLink.addClickHandler(new ClickHandler() {
        
        @Override
        public void onClick(ClickEvent event) {
          executeLoginJs();
        }
      });
      gfcAnchorPanel.clear();
      gfcAnchorPanel.add(signInLink);
    }
  }
  
  public void addViewerFriends(JsArray<JsniPerson> friendsArray) {
    parent.viewerFriendsPanel.clear();
    
    if (0 == friendsArray.length()) {
      parent.viewerFriendsPanel.add(new HTML("You have no friends here :("));
      return;
    }

    parent.viewerFriendsPanel.add(new HTML("Your friends:"));
    for(int i = 0; i < friendsArray.length(); ++i) {
      JsniPerson person = friendsArray.get(i);
      final Image thumbnail = new Image(person.getThumbnailUrl());
      thumbnail.setTitle(person.getDisplayName());
      Anchor link = new Anchor();
      link.setHref(person.getProfileUrl());
      link.setStyleName("fc-thumbnail");
      link.getElement().appendChild(thumbnail.getElement());
      parent.viewerFriendsPanel.add(link);
    }
  }
  
  void addSiteFriends(JsArray<JsniPerson> friendsArray) {
    parent.siteFriendsPanel.clear();
    
    if (0 == friendsArray.length()) {
      return;
    }
    
    parent.siteFriendsPanel.add(new HTML("Friends of CaribbeanVisit:"));
    for(int i = 0; i < friendsArray.length(); ++i) {
      JsniPerson person = friendsArray.get(i);
      final Image thumbnail = new Image(person.getThumbnailUrl());
      thumbnail.setTitle(person.getDisplayName());
      Anchor link = new Anchor();
      link.setHref(person.getProfileUrl());
      link.setStyleName("fc-thumbnail");
      link.getElement().appendChild(thumbnail.getElement());
      parent.siteFriendsPanel.add(link);
    }
  }
}